package com.fancon.android.gardenjokesx.activity;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;

import com.fancon.android.gardenjokesx.GardenJokesBaseFragmentActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.fragment.MyProfileFragment;
import com.fancon.android.gardenjokesx.fragment.PopularFragment;
import com.fancon.android.gardenjokesx.fragment.RecentFragment;
import com.fancon.android.gardenjokesx.fragment.TimeLineFragment;
import com.fancon.android.gardenjokesx.utils.GlobalDelegate;
import com.fancon.android.gardenjokesx.widget.GardenControlView;
import com.fancon.android.gardenjokesx.widget.IRefresh;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitleProvider;

public class GardenJokesActivity1 extends GardenJokesBaseFragmentActivity {
	/** Called when the activity is first created. */
	protected JokeTabsAdapter mAdapter;
	protected ViewPager mPager;
	protected PageIndicator mIndicator;
	private String[] content;
	private GardenControlView topControl;
	private Fragment popular, recent, timeline, myprofile;
	private IRefresh currentIRefresh;
//	private String currentId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			content = getResources().getStringArray(R.array.garden_jokes_tabs);
			setContentView(R.layout.garden_layout1);

			topControl = (GardenControlView) findViewById(R.id.top_control);
			List<Fragment> fragments = new Vector<Fragment>();
			
			popular = new PopularFragment(topControl);
			recent = new RecentFragment(topControl);
			timeline = new TimeLineFragment(topControl);
			myprofile = new MyProfileFragment(topControl);
			
			fragments.add(popular);
			fragments.add(recent);
			fragments.add(timeline);
			fragments.add(myprofile);
			currentIRefresh = (IRefresh) popular;
			
			mAdapter = new JokeTabsAdapter(super.getSupportFragmentManager(), fragments);

			mPager = (ViewPager) findViewById(R.id.pager);
			mPager.setAdapter(mAdapter);

			mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
			mIndicator.setViewPager(mPager);
			mIndicator.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(int position) {
					// TODO Auto-generated method stub
					switch (position) {
					case 0:
						currentIRefresh = (IRefresh) popular;
						break;
					case 1:
						currentIRefresh = (IRefresh) recent;
						break;
					case 2:
						currentIRefresh = (IRefresh) timeline;
						break;
					case 3:
						currentIRefresh = (IRefresh) myprofile;
						break;
					}
					//currentIRefresh.doLoad(null);
				}

				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onPageScrollStateChanged(int arg0) {
					// TODO Auto-generated method stub

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		//currentIRefresh.doLoad(null);
	}

	class JokeTabsAdapter extends FragmentPagerAdapter implements TitleProvider {
		private final List<Fragment> fragments;
		public JokeTabsAdapter(FragmentManager fm, List<Fragment> fragments) {
			super(fm);
			this.fragments = fragments;
		}

		@Override
		public Fragment getItem(int position) {
			Log.d("position", " sdas" + position);
			return this.fragments.get(position);
		}

		@Override
		public int getCount() {
			return this.fragments.size();
		}

		@Override
		public String getTitle(int position) {
			return content[position];
		}
	}

	public IRefresh getCurrentIRefresh() {
		return currentIRefresh;
	}

	public void setCurrentIRefresh(IRefresh currentIRefresh) {
		this.currentIRefresh = currentIRefresh;
	}
	public GardenControlView getTopControl() {
		return topControl;
	}

	public void setTopControl(GardenControlView topControl) {
		this.topControl = topControl;
	}
	@Override
	public void onResume(){
		super.onResume();
		if(GlobalDelegate.isRFM04){
			Log.d("refreshingnnnnnnnnnnnn !", "refreshingnnnnnnnnnnnn");
			getCurrentIRefresh().doRefresh(null);
			GlobalDelegate.isRFM04 = false;
		}
	}
//	@Override
//	public void onBackPressed() {
//		finish();
//	}
//
//	public void onDestroy() {
//		super.onDestroy();
//		//if (currentId != null && currentId.length() >0) {
//			/*
//			 * Notify the system to finalize and collect all objects of the
//			 * application on exit so that the process running the application
//			 * can be killed by the system without causing issues. NOTE: If this
//			 * is set to true then the process will not be killed until all of
//			 * its threads have closed.
//			 */
//			System.runFinalizersOnExit(true);
//
//			/*
//			 * Force the system to close the application down completely instead
//			 * of retaining it in the background. The process that runs the
//			 * application will be killed. The application will be completely
//			 * created as a new application in a new process if the user starts
//			 * the application again.
//			 */
//			System.exit(0);
//		//}
//	}
}